package com.cictec.middleware.tde.rm.protocol.rm.decode;

import java.nio.ByteBuffer;

import org.apache.mina.core.session.IoSession;

import com.cictec.middleware.commons.utils.BinaryUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.tde.exception.MessageDecoderException;
import com.cictec.middleware.tde.rm.protocol.AbstractMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage.Header;
import com.cictec.middleware.tde.rm.protocol.annotion.TerminalMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x24TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.utils.RmBinaryUtils;

/**
 * 刷卡消息解码
 * 
 * @author Zhibin
 *
 */
@TerminalMessageDecoder(0x24)
public class Rm0x24Decoder extends AbstractMessageDecoder {

	@Override
	public TerminalMessage decode(Header header, ByteBuffer in, IoSession session) throws MessageDecoderException {

		Rm0x24TerminalMessage msg = new Rm0x24TerminalMessage(header);
		msg.setCreateTime(RmBinaryUtils.dwordToDateRM(in));
		msg.setReaderMainVersion(BinaryUtils.unsignedByteToInt(in));
		msg.setReaderSubVersion(BinaryUtils.unsignedByteToInt(in));
		
		String flag = PropertiesUtils.getString("terimial.decoder.rfidDecoder", "default");
		if (flag.equals("jm")) {
			msg.setCardNumber(RmBinaryUtils.hexStrToICNumberDoy(in)); // 江门刷卡卡号编码修改
		} else if (flag.equals("dy")) {
			msg.setCardNumber(RmBinaryUtils.dwordToICNumberDoy(in)); // 东营刷卡
		} else {
			msg.setCardNumber(RmBinaryUtils.dwordToICNumber(in));
		}
		long reversed = BinaryUtils.dwordToLong(in);
		msg.setCardType((int) ((reversed >> 8) & 1));
		msg.setOperateType((int) ((reversed >> 9) & 1));
		return msg;
	}

}
